﻿Namespace ControlLibrary
    ''' <summary>
    ''' Provides a tree view of the main WSF objects.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class dbiWSFObjectTree

#Region "Events"
        Public Event NodeClickWarehouse(ByRef Warehouse As dbiWSF.FrameworkObjects.objWarehouse)
        Public Event NodeClickLoadingDock(ByRef LoadingDock As dbiWSF.FrameworkObjects.objLoadingDock, ByVal Button As System.Windows.Forms.MouseButtons)
        Public Event NodeClickShipment(ByRef Shipment As dbiWSF.FrameworkObjects.objShipment, ByVal Button As System.Windows.Forms.MouseButtons)
        Public Event NodeClickOrderHeader(ByRef OrderHeader As dbiWSF.FrameworkObjects.objOrderHeader, ByVal Button As System.Windows.Forms.MouseButtons)
        Public Event NodeClickOrderDetail(ByRef OrderDetail As dbiWSF.FrameworkObjects.objOrderDetail, ByVal Button As System.Windows.Forms.MouseButtons)
        Public Event NodeClickNonDBIWSF(ByRef NodeItem As Dbi.WinControl.dbiNodeItem, ByVal Button As System.Windows.Forms.MouseButtons)
#End Region

#Region "Properties"
        Private l_dbiWSFObject As Object
        Private l_ParentWSFObject As dbiWSF.FrameworkObjects.objWSFObjects
        Private l_TreeLevels As UserInterface.UIDetails.enumTreeLevel
        Private l_AllowAddNew As Boolean = False

        ''' <summary>The main dbiWSFObjects to use with this control.</summary>
        Public Property ParentWSFObject() As dbiWSF.FrameworkObjects.objWSFObjects
            Get
                Return l_ParentWSFObject
            End Get
            Set(ByVal value As dbiWSF.FrameworkObjects.objWSFObjects)
                l_ParentWSFObject = value
            End Set
        End Property

        ''' <summary>The specific dbiWSF object to begin with in the tree.</summary>
        Public Property dbiWSFObject()
            Get
                Return l_dbiWSFObject
            End Get
            Set(ByVal value)
                l_dbiWSFObject = value
            End Set
        End Property

        ''' <summary>The ending tree level to display.</summary>
        Public Property TreeLevels() As UserInterface.UIDetails.enumTreeLevel
            Get
                Return l_TreeLevels
            End Get
            Set(ByVal value As UserInterface.UIDetails.enumTreeLevel)
                l_TreeLevels = value
            End Set
        End Property

        ''' <summary>Determines if the addnew node is displayed.</summary>
        Public Property AllowAddNew() As Boolean
            Get
                Return l_AllowAddNew
            End Get
            Set(ByVal value As Boolean)
                l_AllowAddNew = value
            End Set
        End Property

#End Region
#Region "Load Data"

        ''' <summary>Loads the tree starting with the specified object to the TreeLevelsToUse.</summary>
        ''' <param name="dbiWSFObject">The dbiWSF Object to begin the tree.</param>
        ''' <param name="TreeLevelsToUse">The end level of the tree to display to.</param>
        ''' <param name="myWSFObject">The main dbiWSFObjects to use for this control.</param>
        Public Sub LoadObject(ByRef dbiWSFObject As Object, ByVal TreeLevelsToUse As UserInterface.UIDetails.enumTreeLevel, ByRef myWSFObject As dbiWSF.FrameworkObjects.objWSFObjects)

            dbiWSFObject = dbiWSFObject
            TreeLevels = TreeLevelsToUse
            ParentWSFObject = myWSFObject

            Select Case dbiWSFObject.GetType.ToString
                Case GetType(dbiWSF.FrameworkObjects.objWSFObjects).ToString
                    DisplayWarehouse(dbiWSFObject)
                Case GetType(dbiWSF.FrameworkObjects.objWarehouse).ToString
                    DisplayLoadingDocks(dbiWSFObject)
                Case GetType(dbiWSF.FrameworkObjects.objLoadingDock).ToString
                    DisplayShipments(dbiWSFObject)
                Case GetType(dbiWSF.FrameworkObjects.objShipment).ToString
                    DisplayOrderHeaders(dbiWSFObject)
            End Select
        End Sub
#End Region
       
        ''' <summary>Supporting Method: Parses the dbiWSF Object into a tree node structure.</summary>
        Private Sub DisplayWarehouse(ByRef dbiWSFObjectsToDisplay As dbiWSF.FrameworkObjects.objWSFObjects)
            Me.dbiListTree.TitleText = "Warehouse"

            Dim treeValue As UserInterface.UIDetails.enumTreeLevel = GetTreeActual()

            For Each warehouse As dbiWSF.FrameworkObjects.objWarehouse In dbiWSFObjectsToDisplay.Warehouses
                Dim newWarehouseNode As New Dbi.WinControl.dbiNodeItem
                newWarehouseNode.Text = warehouse.Description + " | " + warehouse.Location
                newWarehouseNode.Tag = warehouse
                Me.dbiListTree.Items.Add(newWarehouseNode)

                If treeValue > UserInterface.UIDetails.enumTreeLevel.Warehouse Then
                    For Each loadingDock As dbiWSF.FrameworkObjects.objLoadingDock In warehouse.LoadingDocks
                        Dim newLoadingDockNode As New Dbi.WinControl.dbiNodeItem
                        newLoadingDockNode.Text = loadingDock.Description + " | Position: " + loadingDock.WarehouseOrdinal.ToString
                        newLoadingDockNode.Tag = loadingDock
                        newWarehouseNode.Items.Add(newLoadingDockNode)

                        If treeValue > UserInterface.UIDetails.enumTreeLevel.LoadingDock Then
                            For Each shipment As dbiWSF.FrameworkObjects.objShipment In loadingDock.Shipments
                                Dim newShipmentNode As New Dbi.WinControl.dbiNodeItem
                                newShipmentNode.Text = shipment.Description
                                newShipmentNode.Tag = shipment
                                newLoadingDockNode.Items.Add(newShipmentNode)

                                If treeValue > UserInterface.UIDetails.enumTreeLevel.Shipment Then
                                    For Each orderheader As dbiWSF.FrameworkObjects.objOrderHeader In shipment.OrderHeaders
                                        Dim newOrderHeaderNode As New Dbi.WinControl.dbiNodeItem
                                        newOrderHeaderNode.Text = orderheader.OrderEntryType + " | " + orderheader.OrderEntryID
                                        newOrderHeaderNode.Tag = orderheader
                                        newShipmentNode.Items.Add(newOrderHeaderNode)

                                        If treeValue > UserInterface.UIDetails.enumTreeLevel.OrderHeader Then
                                            For Each orderdetail As dbiWSF.FrameworkObjects.objOrderDetail In orderheader.OrderDetails
                                                Dim newOrderDetailNode As New Dbi.WinControl.dbiNodeItem
                                                newOrderDetailNode.Text = orderdetail.LineNumber
                                                newOrderDetailNode.Tag = orderdetail
                                                newOrderHeaderNode.Items.Add(newOrderDetailNode)
                                            Next
                                        End If
                                    Next
                                End If
                            Next
                        End If
                    Next
                End If
            Next
            If AllowAddNew = True Then
                Dim newWhse As New Dbi.WinControl.dbiNodeItem
                newWhse.Text = "<Add New Warehouse>"
                newWhse.Font = New System.Drawing.Font(Me.dbiListTree.Font, Drawing.FontStyle.Italic + Drawing.FontStyle.Bold)
                newWhse.ForeColor = Drawing.Color.DarkGray
                newWhse.Tag = "AddNewWarehouse"
                Me.dbiListTree.Items.Add(newWhse)
            End If
        End Sub

        ''' <summary>Supporting Method: Parses the dbiWSF Object into a tree node structure.</summary>
        Private Sub DisplayLoadingDocks(ByRef WarehouseToDisplay As dbiWSF.FrameworkObjects.objWarehouse)
            Me.dbiListTree.TitleText = "Warehouse " + WarehouseToDisplay.Description

            Dim treeValue As Integer = GetTreeActual()

            For Each loadingDock As dbiWSF.FrameworkObjects.objLoadingDock In WarehouseToDisplay.LoadingDocks
                Dim newLoadingDockNode As New Dbi.WinControl.dbiNodeItem
                newLoadingDockNode.Text = loadingDock.Description + " | Position: " + loadingDock.WarehouseOrdinal.ToString
                newLoadingDockNode.Tag = loadingDock
                Me.dbiListTree.Items.Add(newLoadingDockNode)

                If treeValue > UserInterface.UIDetails.enumTreeLevel.LoadingDock Then
                    For Each shipment As dbiWSF.FrameworkObjects.objShipment In loadingDock.Shipments
                        Dim newShipmentNode As New Dbi.WinControl.dbiNodeItem
                        newShipmentNode.Text = shipment.Description
                        newShipmentNode.Tag = shipment
                        newLoadingDockNode.Items.Add(newShipmentNode)

                        If treeValue > UserInterface.UIDetails.enumTreeLevel.Shipment Then
                            For Each orderheader As dbiWSF.FrameworkObjects.objOrderHeader In shipment.OrderHeaders
                                Dim newOrderHeaderNode As New Dbi.WinControl.dbiNodeItem
                                newOrderHeaderNode.Text = orderheader.OrderEntryType + " | " + orderheader.OrderEntryID
                                newOrderHeaderNode.Tag = orderheader
                                newShipmentNode.Items.Add(newOrderHeaderNode)

                                If treeValue > UserInterface.UIDetails.enumTreeLevel.OrderHeader Then
                                    For Each orderdetail As dbiWSF.FrameworkObjects.objOrderDetail In orderheader.OrderDetails
                                        Dim newOrderDetailNode As New Dbi.WinControl.dbiNodeItem
                                        newOrderDetailNode.Text = orderdetail.LineNumber
                                        newOrderDetailNode.Tag = orderdetail
                                        newOrderHeaderNode.Items.Add(newOrderDetailNode)
                                    Next
                                End If
                            Next
                        End If
                    Next
                End If
            Next

        End Sub

        ''' <summary>Supporting Method: Parses the dbiWSF Object into a tree node structure.</summary>
        Private Sub DisplayShipments(ByRef LoadingDockToDisplay As dbiWSF.FrameworkObjects.objLoadingDock)
            Me.dbiListTree.TitleText = "Warehouse: " + ParentWSFObject.GetWarehouseFromGUID(LoadingDockToDisplay.WarehouseID).Description + _
                                        vbCrLf + "Loading Dock " + LoadingDockToDisplay.Description
            Me.dbiListTree.TitleFont = New System.Drawing.Font(Me.dbiListTree.TitleFont.FontFamily, 12, Drawing.FontStyle.Bold)
            Dim treeValue As Integer = GetTreeActual()

            For Each shipment As dbiWSF.FrameworkObjects.objShipment In LoadingDockToDisplay.Shipments
                Dim newShipmentNode As New Dbi.WinControl.dbiNodeItem
                newShipmentNode.Text = shipment.Description
                newShipmentNode.Tag = shipment
                Me.dbiListTree.Items.Add(newShipmentNode)

                If treeValue > UserInterface.UIDetails.enumTreeLevel.Shipment Then
                    For Each orderheader As dbiWSF.FrameworkObjects.objOrderHeader In shipment.OrderHeaders
                        Dim newOrderHeaderNode As New Dbi.WinControl.dbiNodeItem
                        newOrderHeaderNode.Text = orderheader.OrderEntryType + " | " + orderheader.OrderEntryID
                        newOrderHeaderNode.Tag = orderheader
                        newShipmentNode.Items.Add(newOrderHeaderNode)

                        If treeValue > UserInterface.UIDetails.enumTreeLevel.OrderHeader Then
                            For Each orderdetail As dbiWSF.FrameworkObjects.objOrderDetail In orderheader.OrderDetails
                                Dim newOrderDetailNode As New Dbi.WinControl.dbiNodeItem
                                newOrderDetailNode.Text = orderdetail.LineNumber
                                newOrderDetailNode.Tag = orderdetail
                                newOrderHeaderNode.Items.Add(newOrderDetailNode)
                            Next
                        End If
                    Next
                End If
            Next
        End Sub

        ''' <summary>Supporting Method: Parses the dbiWSF Object into a tree node structure.</summary>
        Private Sub DisplayOrderHeaders(ByRef ShipmentToDisplay As dbiWSF.FrameworkObjects.objShipment)
            Me.dbiListTree.TitleText = "Loading Dock " + ParentWSFObject.GetLoadingDockFromGUID(ShipmentToDisplay.LoadingDockID).Description + _
                                        vbCrLf + "Shipment " + ShipmentToDisplay.Description
            Me.dbiListTree.TitleFont = New System.Drawing.Font(Me.dbiListTree.TitleFont.FontFamily, 12, Drawing.FontStyle.Bold)
            Dim treeValue As Integer = GetTreeActual()

            For Each orderheader As dbiWSF.FrameworkObjects.objOrderHeader In ShipmentToDisplay.OrderHeaders
                Dim newOrderHeaderNode As New Dbi.WinControl.dbiNodeItem
                newOrderHeaderNode.Text = orderheader.OrderEntryType + " | " + orderheader.OrderEntryID
                newOrderHeaderNode.Tag = orderheader
                Me.dbiListTree.Items.Add(newOrderHeaderNode)

                If treeValue > UserInterface.UIDetails.enumTreeLevel.OrderHeader Then
                    For Each orderdetail As dbiWSF.FrameworkObjects.objOrderDetail In orderheader.OrderDetails
                        Dim newOrderDetailNode As New Dbi.WinControl.dbiNodeItem
                        newOrderDetailNode.Text = orderdetail.LineNumber
                        newOrderDetailNode.Tag = orderdetail
                        newOrderHeaderNode.Items.Add(newOrderDetailNode)
                    Next
                End If
            Next
        End Sub

        ''' <summary>Supporting Method: Gets the current actual level of the tree that is visible.</summary>
        Private Function GetTreeActual() As UserInterface.UIDetails.enumTreeLevel
            Dim treeValue As UserInterface.UIDetails.enumTreeLevel
            If TreeLevels < 1 Then
                treeValue = UserInterface.UIDetails.enumTreeLevel.All
            Else
                treeValue = TreeLevels
            End If
            Return treeValue
        End Function

        ''' <summary>Supporting Method: Raises the node click events.</summary>
        Private Sub dbiListTree_NodeClick(ByVal sender As Object, ByVal e As Dbi.WinControl.NodeClickEventArgs) Handles dbiListTree.NodeClick

            Try
                If e.Item IsNot Nothing Then
                    If e.Item.Tag IsNot Nothing Then
                        Select Case e.Item.Tag.GetType.ToString
                            Case GetType(dbiWSF.FrameworkObjects.objWarehouse).ToString
                                RaiseEvent NodeClickWarehouse(e.Item.Tag)
                            Case GetType(dbiWSF.FrameworkObjects.objLoadingDock).ToString
                                RaiseEvent NodeClickLoadingDock(e.Item.Tag, e.Button)
                            Case GetType(dbiWSF.FrameworkObjects.objShipment).ToString
                                RaiseEvent NodeClickShipment(e.Item.Tag, e.Button)
                            Case GetType(dbiWSF.FrameworkObjects.objOrderHeader).ToString
                                RaiseEvent NodeClickOrderHeader(e.Item.Tag, e.Button)
                            Case GetType(dbiWSF.FrameworkObjects.objOrderDetail).ToString
                                RaiseEvent NodeClickOrderDetail(e.Item.Tag, e.Button)
                            Case Else
                                RaiseEvent NodeClickNonDBIWSF(e.Item, e.Button)
                        End Select
                    Else
                        RaiseEvent NodeClickNonDBIWSF(e.Item, e.Button)
                    End If
                End If
            Catch ex As Exception
                Throw New Exception("UI Object Tree: Node Click Error", ex) '<Generic Error> Modify as needed
            End Try
        End Sub
    End Class
End Namespace